const http = require('http');
function createRequest(path, method, data, callback, errorcallback) {
    return new Promise((resolve, reject) => {
        let ne_req = ''
        const http_client = http.request({
            hostname: 'api.map.baidu.com',
            method: method,
            path: path,
            timeout: 5000,
            headers: {
                'Accept': '*/*',
                'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
                'Connection': 'keep-alive',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Referer': 'http://api.map.baidu.com',
                'Host': 'api.map.baidu.com',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36',
            }
        }, res => {
            console.log('STATUS: ' + res.statusCode);
            console.log('HEADERS: ' + JSON.stringify(res.headers));
            res.setEncoding('utf8')
            res.on('error', err => {
                reject(err)
            })
            res.on('data', chunk => {
                ne_req += chunk
            })
            res.on('end', () => {
                resolve(ne_req)
            })
        })
        if (method == 'POST') {
            http_client.write(data)
        }
        http_client.end()
    })
}
module.exports = {
    createRequest
}